/*
 *  SyncIT , easy synchronization of files between computers and devices.
 *  Copyright (C) 2011  Christian Masus <cmasus(at)gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version. If redistributing and/or modifying
 *  this file, this notice must be included in the redistributed or
 *  modified copy.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package log;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import config.ConfigInterface;

/**
 * Interface for logging of messages and retrieving of the logged messages.
 * Logged messages will be stored permanently in the config folder.
 * @author Christian Masus
 *
 */
public class Log implements LogInterface {

	public static final String LOG_WARNING = "warning";
	public static final String LOG_ERROR   = "error";
	public static final String LOG_INFO    = "info";

	private HandleLogFiles hlf;
	private boolean handleLogFilesInitiated;
	private SimpleDateFormat format;
	private HashMap<String, LogRestrictions> logReceivers;
	private ArrayList<String> logSubscribers;

	public Log(ConfigInterface config) {
		hlf = new HandleLogFiles(config);
		handleLogFilesInitiated = hlf.init();
		format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS");
		logReceivers = new HashMap<String, LogRestrictions>();
		logSubscribers = new ArrayList<String>();
	}


	public void log(Date date, String type, String message) {
		String logStr = format.format(date) + ": [" + type + "] " + message + "\n";
		if(handleLogFilesInitiated) { // Avoid crash if we cannot write log to disk
			hlf.log(logStr);
		}
		for(int i = 0; i < logSubscribers.size(); i++) { // Send log to subscribers
			LogRestrictions restrictions = logReceivers.get(logSubscribers.get(i));
			// TODO if passes through receiver filter, then send.
			restrictions.getReceiver().sendLogLine(logStr);
		}
	}

	public void logError(Date date, String message) {
		log(date, LOG_ERROR, message);
	}

	public void logWarning(Date date, String message) {
		log(date, LOG_WARNING, message);
	}

	public void logInfo(Date date, String message) {
		log(date, LOG_INFO, message);
	}




	public void registerLogReceivers(String regName, LogRestrictions restrictions) {
		logReceivers.put(regName, restrictions);
	}

	public void unregisterLogReceivers(String regName) {
		logSubscribers.remove(regName); // Just in case
		logReceivers.remove(regName);
	}

	public void changeLogReceiversRestrictions(String regName, LogRestrictions restrictions) {
		logReceivers.put(regName, restrictions);
	}

	public void askForLogHistory(String regName) {
		//TODO
	}

	public void subscribeOnLog(String regName) {
		logSubscribers.add(regName);
	}
	public void stopLogSubscription(String regName) {
		logSubscribers.remove(regName);
	}
}
